package com.isec.base.monit.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.common.EncryptUtil;
import com.core.tools.DBTool;
import com.core.tools.FinalVarStaticTool;
import com.core.tools.PropertiesTool;
import com.isec.base.monit.dao.PmDao;
import com.isec.base.monit.dao.SourceDao;
import com.isec.util.KettleUtil;

import javax.annotation.PostConstruct;

@EnableScheduling//开启定时任务
@Component
public class TimerTaskService {

	@Autowired
	PmDao pmDao;

	@Autowired
	PmService pmService;

	@Autowired
	Environment env;

	@Autowired
	SourceService sourceService;

	@Autowired
	SourceDao sourceDao;

	/**
	 * 数据防篡改校验频率
	 * @Author ldonglit
	 * @Date 2024年3月20日20:40:06
	 * @TODO 每五分钟校验
	 */
	@Scheduled(cron="0 */5 * * * ? ")//每5分钟出发一次
	public void cron(){
		if (FinalVarStaticTool.ISUPLOAD_DATA) {
			return;
		}
		FinalVarStaticTool.ISUPLOAD_DATA = true;
		List<Map<String, Object>> dbs = sourceDao.getMapList("select * from idgar_dbs_tab");
		JdbcTemplate jdbc = null;
		List<String[]> flag = new ArrayList<String[]>();
		for (int i = 0; i < dbs.size(); i++) {
			String un = dbs.get(i).get("db_user").toString();
			String up = dbs.get(i).get("db_pwd").toString();
			String url =  dbs.get(i).get("db_url").toString();
			try {
				if (DBTool.isConnect(un, up, url)) {
					jdbc = DBTool.getJdbctemplate(un,up,url,"mysql");
					flag.add(new String[] {dbs.get(i).get("db_name").toString(),"2",sourceService.checkIsChange(jdbc) + "",un,up,url});
					flag.add(new String[] {dbs.get(i).get("db_name").toString(),"1",pmService.checkIsChange(jdbc)+"",un,up,url});
				}
			} catch (Exception e) {
				System.out.println("数据库连接异常");
			}
		}

		for (int i = 0; i < flag.size(); i++) {
			String[] val = flag.get(i);
			if (StringUtils.equals(val[1] , "2") && !StringUtils.equals(val[2] , "0")) {//表示该节点数据被篡改，去未被篡改的节点获取数据，同步
				for (int j = 0; j < flag.size(); j++) {
					String[] val1 = flag.get(j);
					if (StringUtils.equals(val1[1], "2") && StringUtils.equals(val1[2], "0")) {//表示该节点数据正常
						synData("source",val1[5],val1[4],val[5],val[4]);
						break;
					}
				}
			}
			if (StringUtils.equals(val[1] , "1") && !StringUtils.equals(val[2] , "0")) {//表示该节点数据被篡改，去未被篡改的节点获取数据，同步
				for (int j = 0; j < flag.size(); j++) {
					String[] val1 = flag.get(j);
					if (StringUtils.equals(val1[1], "1") && StringUtils.equals(val1[2], "0")) {//表示该节点数据正常
						synData("pm",val1[5],val1[4],val[5],val[4]);
						break;
					}
				}
			}
		}
		FinalVarStaticTool.ISUPLOAD_DATA = false;
	}

	private void synData(String t,String ydb_url,String ydb_pwd,String tdb_url,String tdb_pwd) {
		long start = System.currentTimeMillis();
		Map<String, String> map = new HashMap<String, String>();
		String ydb_ip = ydb_url.split(":")[2].replaceAll("//", "");
		String tdb_ip = tdb_url.split(":")[2].replaceAll("//", "");
		map.put("ydb_ip", ydb_ip);
		map.put("ydb_pwd", EncryptUtil.encryptPassword(ydb_pwd));
		map.put("tdb_ip", tdb_ip);
		map.put("tdb_pwd", EncryptUtil.encryptPassword(tdb_pwd));
		KettleUtil.runKettleTransfer(map,PropertiesTool.getString("kettle.ktr."+t+".path"));
		KettleUtil.runKettleTransfer(map,PropertiesTool.getString("kettle.datalog."+t+".path"));
		System.out.println("transfer data time:" + ( System.currentTimeMillis() - start ));
	}

	@PostConstruct
	public void initCacheData(){

	}

}
